<script setup lang="ts">
import {useRoute} from "vue-router";
import router from "@/router/index.js";
import {onMounted, ref} from "vue";
import {getRoleByUserIdAPI} from "@/apis/roleInfo.js";
import {getByUserIdAPI} from "@/apis/permissionInfo.js";
import {getUserDetailByIdAPI} from "@/apis/userManage.js";
import {getOneLocationAPI} from "@/apis/location.js";

const route = useRoute();
interface userType {
  loginName: string;
  realName: string;
  phone: string;
  email: string;
  createBy: string;
  lastUpdateBy: string;
  createTime: number;
  updateTime: number;
  profilephoto: string;
  id: number;
}




const userInfo=ref<userType>({
  id:0,
  loginName:"",
  realName:"",
  phone:"",
  email:"",
  createBy:"",
  lastUpdateBy:"",
  createTime:0,
  updateTime:0,
  profilephoto:""

})

const roleInfo=ref<any>([]);
const permissionInfo=ref<any>([]);

const roleCheckedList=ref<any>([]);
const permissionCheckedList=ref<any>([]);

// 定义 item 的类型
interface RoleItem {
  roleId: number;
}

// 定义 permissionItem 的类型
interface PermissionItem {
  permissionId: number;
}

const locationInfo=ref<any>({});


onMounted(async () => {
  if(route.params.id===null||route.params.id===undefined){
    await router.push("/manage/userManager");
  }
  else {
    const res=  await getUserDetailByIdAPI(Number(route.params.id));
    userInfo.value=res.data;
  }

  await getOneLocationAPI(Number(route.params.id)).then(res=>{
    locationInfo.value=res.data;
    console.log(locationInfo.value);
  })





  const res_userRoles=await getRoleByUserIdAPI(Number(route.params.id));
  res_userRoles.data.forEach((item:RoleItem)=>{
    roleInfo.value.push(role[item.roleId-1]);
    roleCheckedList.value.push(role[item.roleId-1]);
  })

  const res_user_permission=await getByUserIdAPI(Number(route.params.id) );
  res_user_permission.data.forEach((item:PermissionItem)=>{
    permissionInfo.value.push(permission[item.permissionId-1]);
    permissionCheckedList.value.push(permission[item.permissionId-1]);
  })


})

// 时间戳变成真实时间
function getDate(time: number) {
  let date = new Date(time);
  let Y=date.getFullYear()+'年';
  let M=(date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '月';
  let D=date.getDate()+'日';
  let h=date.getHours()+'时';
  let m=date.getMinutes()+'分';
  return Y+M+D+h+m;
}

enum  role{
  "程序员","超级管理员","管理员","系统管理者","普通用户","访客"
}

enum permission{
  "新增","删除","查询","修改","访问"
}

// const checkList=ref([])
//
// const roleIdList=ref([]);

// const getRoleIdList=()=>{
//   roleIdList.value=[];
//   checkList.value.forEach((item:string)=>{
//     roleIdList.value.push(role[item]);
//   })
// }


</script>

<template>
  <!-- 面包屑 -->
  <div class="bread-container">
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: `/` }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:`/manage/userManager`}" >用户列表</el-breadcrumb-item>
      <el-breadcrumb-item  >用户详细页</el-breadcrumb-item>
    </el-breadcrumb>
  </div>

  <div class="divTable">
    <table class="table">
      <tbody>
      <tr>
        <td class="tableTitle" colspan="4" >用户信息</td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF;color: #242424;font-family: 微软雅黑,serif">用户名称</td>
        <td style="width: 350px;height: 15px;   text-align: center  ">{{userInfo.loginName}}</td>
        <td style="width: 250px;height: 15px; text-align: center ;background: #EFF6FF"  >真实姓名</td>
        <td style="width: 350px;height: 15px ; text-align: center   ">{{userInfo.realName}}</td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">联系电话</td>
        <td style="width: 350px;height: 15px;text-align: center  ">{{userInfo.phone}}</td>
        <td style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >电子邮箱</td>
        <td style="width: 350px;height: 15px;text-align: center   ">{{userInfo.email}}</td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">创建人</td>
        <td style="width: 350px;height: 15px;text-align: center  ">{{userInfo.createBy}}</td>
        <td style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >最后修改人</td>
        <td style="width: 350px;height: 15px;text-align: center   ">{{userInfo.lastUpdateBy}}</td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">创建时间</td>
        <td style="width: 350px;height: 15px;text-align: center  ">{{getDate(userInfo.createTime)}}</td>
        <td style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >最后修改时间</td>
        <td style="width: 350px;height: 15px;text-align: center   ">{{ getDate(userInfo.updateTime)}}</td>
      </tr>

      <tr>
        <td colspan="4" style="width: 250px;height: 15px;text-align: center;background: #EFF6FF"><h3 style="font-family: 'JetBrains Mono',serif;font-weight: bold">用户的角色和权限</h3></td>
      </tr>
      <tr>
        <td  style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">用户角色列表</td>
        <td colspan="3" style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">
          <el-checkbox-group v-model="roleCheckedList">
            <!--            后端获取到当前用户的角色列表，并显示-->
            <el-checkbox v-for="(item,index) in roleInfo" :key="index" :label="item" checked disabled  >
              {{item}}
            </el-checkbox>
          </el-checkbox-group>
        </td>
      </tr>

      <tr>
        <td  style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">用户权限列表</td>
        <td colspan="3" style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">
          <el-checkbox-group v-model="permissionCheckedList">
            <!--            后端获取到当前用户的角色列表，并显示-->
            <el-checkbox v-for="(item,index) in permissionInfo" :key="index" :label="item" checked disabled  >
              {{item}}
            </el-checkbox>
          </el-checkbox-group>
        </td>
      </tr>
<!--      <tr>-->
<!--        <td colspan="4" style="width: 250px;height: 15px;text-align: center;background: #EFF6FF"><h3 style="font-family: 'JetBrains Mono',serif;font-weight: bold">用户所在地</h3></td>-->
<!--      </tr>-->
<!--      <tr>-->
<!--        <td  style="width: 250px;height: 15px;text-align: center;background: #EFF6FF"><h3 style="font-family: 'JetBrains Mono',serif;font-weight: bold">用户所在项目</h3></td>-->
<!--        <td  style="width: 250px;height: 15px;text-align: center;"><h3 style="font-family: 'JetBrains Mono',serif;font-weight: bold">{{locationInfo.projectName}}</h3></td>-->
<!--        <td  style="width: 250px;height: 15px;text-align: center;background: #EFF6FF"><h3 style="font-family: 'JetBrains Mono',serif;font-weight: bold">用户所在仓库</h3></td>-->
<!--        <td  style="width: 250px;height: 15px;text-align: center;"><h3 style="font-family: 'JetBrains Mono',serif;font-weight: bold">{{locationInfo.warehouseName}}</h3></td>-->
<!--      </tr>-->


      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">用户头像</td>
        <td  colspan="3" style="width: 350px;height: 15px;text-align: center  ">
          <br>
          <div class="demo-image__preview">
            <el-image
                style="width: 100px; height: 100px"
                :src="userInfo.profilephoto"
                :zoom-rate="1.2"
                :max-scale="5"
                :min-scale="0.5"
                fit="cover"
            />
          </div>
          <!--          <el-button link type="primary" @click="downPhotos" >打包下载</el-button>-->
          <br>
        </td>
      </tr>
      </tbody>
    </table>
  </div>
</template>

<style scoped lang="scss">

.bread-container {
  padding: 25px 10px;
  margin-left: 10px;
}
.divTable {
  position: static;
  top: 150px;
  bottom: 0;
  left: 300px;
  right: 0;
  margin: auto;
}
table, td,tr{
  border: 1px solid #44a8da;
  text-align: center;
  border-spacing: 0;
  border-collapse: collapse;
  height: 45px;
}

.tableTitle{

  border-right: 0 none;
  border-left: 0 none;
  text-align: center;
  font-size: large;
  font-weight: bold;
}

.demo-image__error .image-slot {
  font-size: 30px;
}
.demo-image__error .image-slot .el-icon {
  font-size: 30px;
}
.demo-image__error .el-image {
  width: 100%;
  height: 200px;
}
</style>